package com.hxwm.hwdbtool.entity.dbconnect;

import com.hxwm.hwdbtool.util.HwUtil;

import java.sql.Connection;
import java.sql.DriverManager;

public class HwDateSource {
    private String driverType;
    private String dbUrl;
    private String dbIp;
    private String dbPort;
    private String dbName;
    private String dbUser;
    private String dbPassword;

    public HwDateSource() {
    }

    /**
     * 创建数据库连接对象，使用基本信息单元创建
     * @param dbType
     * @param ip
     * @param port
     * @param dbname
     * @param user
     * @param password
     */
    public HwDateSource(String dbType,String ip,String port,String dbname, String user, String password) {
        //jdbc:mysql://localhost:3306/hwdemo
        String url = "jdbc:" + dbType + "://" + ip + ":" + port + "/" + dbname;
        this.dbUrl = url;
        this.dbIp = ip;
        this.dbPort = port;
        this.dbName = dbname;
        this.dbUser = user;
        this.dbPassword = password;
        this.driverType = dbType;
    }

    /**
     * 创建数据库连接对象
     * @param url
     * @param user
     * @param password
     */
    public HwDateSource(String url, String user, String password) {

        if(!HwUtil.isNullOrEmpty(url)){
            this.dbUrl = url;
            String[] dbInfoArray = dbUrl.split("[:/]+");
            int dbInfoArrayLength = dbInfoArray.length;
            if(dbInfoArrayLength >= 2){
                this.driverType = dbInfoArray[1];
            }
            if(dbInfoArrayLength >= 3){
                this.dbIp = dbInfoArray[2];
            }
            if(dbInfoArrayLength >= 4){
                this.dbPort = dbInfoArray[3];
            }
            if(dbInfoArrayLength >= 5){
                this.dbName = dbInfoArray[4];
            }
            this.dbUser = user;
            this.dbPassword = password;
        }
    }

    /**
     * 获取数据库连接
     * @return
     * @throws Exception
     */
    public Connection getConnection() throws Exception {
        if("db2".equals(this.driverType)) {
            Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
        }else if("mysql".equals(this.driverType)){
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
        }else if("oracle".equals(this.driverType)){
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
        }
        return DriverManager.getConnection(dbUrl,dbUser,dbPassword);
    }

    public String getDriverType() {
        return driverType;
    }

    public void setDriverType(String driverType) {
        this.driverType = driverType;
    }

    public String getDbUrl() {
        return dbUrl;
    }

    public void setDbUrl(String dbUrl) {
        this.dbUrl = dbUrl;
    }

    public String getDbIp() {
        return dbIp;
    }

    public void setDbIp(String dbIp) {
        this.dbIp = dbIp;
    }

    public String getDbPort() {
        return dbPort;
    }

    public void setDbPort(String dbPort) {
        this.dbPort = dbPort;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getDbUser() {
        return dbUser;
    }

    public void setDbUser(String dbUser) {
        this.dbUser = dbUser;
    }

    public String getDbPassword() {
        return dbPassword;
    }

    public void setDbPassword(String dbPassword) {
        this.dbPassword = dbPassword;
    }
}